package com.luhuiguo.cms.server.util;

import java.io.UnsupportedEncodingException;
import java.net.URI;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class PathUtils {

    public static final String SLASH = "/";

    public static final String SITE = "site";

    public static final String SITE_PREFIX = SITE + SLASH;

    public static final String ARTICLE = "article";

    public static final String ARTICLE_PREFIX = "/a-";

    public static final String CHANNEL = "channel";

    public static final String INDEX = "index";

    public static final String SLASH_SITE = SLASH + SITE;

    public static final String SLASH_SITE_SLASH = SLASH_SITE + SLASH;

    public static final String SLASH_CHANNEL = SLASH + CHANNEL;

    public static final String SLASH_ARTICLE = SLASH + ARTICLE;

    public static final String SLASH_INDEX = SLASH + INDEX;

    public static final String TPL_SUFFIX = ".html";

    public static final String ARTICLE_TPL = ARTICLE + TPL_SUFFIX;

    public static final String CHANNEL_TPL = CHANNEL + TPL_SUFFIX;

    public static final String INDEX_TPL = INDEX + TPL_SUFFIX;

    public static String relativizePath(String base, String path) {
        return URI.create(base).relativize(URI.create(path)).normalize().getPath();

    }

    public static String resolvePath(String base, String path) {
        return URI.create(base).resolve(path).normalize().getPath();
    }

    public static String normalizePath(String path) {
        return URI.create(path).normalize().getPath();
    }

    public static String removeSitePerfix(String path) {
        return StringUtils.removeStart(path, SLASH_SITE);
    }

    public static String getArticleViewName(String path) {
        return StringUtils.substringBeforeLast(removeSitePerfix(path), ARTICLE_PREFIX) + SLASH_ARTICLE;
    }

    public static String getChannelViewName(String path) {
        return StringUtils.substringBeforeLast(removeSitePerfix(path), SLASH_INDEX) + SLASH_INDEX;
    }

    public static String getParentName(String name) {

        boolean isIndex = name.endsWith(INDEX_TPL);
        boolean isChannel = name.endsWith(CHANNEL_TPL);
        boolean isArticle = name.endsWith(ARTICLE_TPL);

        if (!(isIndex || isChannel || isArticle)) {
            return null;
        }
        if (isIndex) {
            name = StringUtils.removeEnd(name, INDEX_TPL).concat(CHANNEL_TPL);
        }
        String[] paths = StringUtils.split(name, SLASH);
        if (paths.length > 1) {
            paths = (String[]) ArrayUtils.remove(paths, paths.length - 2);
            return StringUtils.join(paths, SLASH);
        } else {
            return null;
        }
    }

    public static String getChannelPath(String path) {
        path = StringUtils.substringBeforeLast(removeSitePerfix(path), INDEX);

        if (!StringUtils.startsWith(path, "/")) {
            path = "/" + path;

        }
        if (!StringUtils.endsWith(path, "/")) {
            path = path + "/";
        }

        return path;
    }

    public static String encodingFix(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return str;
        }
    }

}
